home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / QuickDraw3D 1.6 SDK / Mac SampleCode New for 1.6 / Unsupported Libraries / PictRead.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  7.4 KB  |  365 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PictRead.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    1.6
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Robert Dierkes
  13.  
  14.         Other Contact:        Brian Greenstone
  15.  
  16.         Technology:            QuickDraw 3D
  17.  
  18.     Writers:
  19.  
  20.         (BG)    Brian Greenstone
  21.  
  22.     Change History (most recent first):
  23.  
  24.      <QD3D5>    10/15/98    BG        Support Compressed Pixmaps
  25. */
  26.  
  27. /******************************************************************************
  28.  **                                                                             **
  29.  **     Module:        PictRead.c                                                 **
  30.  **                                                                          **
  31.  **                                                                          **
  32.  **     Purpose:                                                               **
  33.  **                                                                          **
  34.  **                                                                          **
  35.  **                                                                          **
  36.  **     Copyright (C) 1992-1995 Apple Computer, Inc.  All rights reserved.     **
  37.  **                                                                          **
  38.  **                                                                          **
  39.  *****************************************************************************/
  40.  
  41. #include <QuickTimeComponents.h>
  42.  
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46.  
  47. #include <StandardFile.h>
  48. #include <Memory.h>
  49.  
  50. #include <QDOffscreen.h>
  51. #include <ImageCompression.h>
  52. #include "QD3D.h"
  53. #include "QD3DStorage.h"
  54. #include "QD3DShader.h"
  55. #include "PictRead.h"
  56.  
  57.  
  58. /*===========================================================================*\
  59.  *
  60.  *    Routine:    OpenPICTFile()
  61.  *
  62.  *    Comments:    open a PICT file and read it into a PICT handle
  63.  *
  64. \*===========================================================================*/
  65.  
  66. PicHandle OpenPICTFile(
  67.     FSSpecPtr fileSpec)
  68. {
  69.     short        fRefNum;
  70.     OSErr        err;
  71.     long        curEOF;
  72.     PicHandle    myPic;
  73.     long         count;
  74.     Ptr         buffer;
  75.     
  76.     /* open PICT file */
  77.     err = FSpOpenDF(fileSpec, fsCurPerm, &fRefNum);
  78.     if (err != 0) {
  79.         /*printf("Error - cannot open file\n"); */
  80.         exit(-1);
  81.     }
  82.     
  83.     /* get size of file */
  84.     err = GetEOF(fRefNum, &curEOF);
  85.     if (err != 0) {
  86.         /*printf("Error - cannot get EOF\n"); */
  87.         exit(-2);
  88.     }
  89.     
  90.     /* move the file mark to 512 */
  91.     err = SetFPos(fRefNum, fsFromStart, 512L);
  92.     if (err != 0) {
  93.         /*printf("Error - cannot seek 512\n"); */
  94.         exit(-3);
  95.     }
  96.  
  97.     /* size of data to read */
  98.     count = curEOF - 512;
  99.     
  100.     /* create the PicHandle */
  101.     myPic = (PicHandle)NewHandle(count);
  102.     HLock((Handle)myPic);
  103.     
  104.     /* read the PICT info */
  105.     buffer = (Ptr)(*myPic);
  106.     err = FSRead(fRefNum, &count, buffer);
  107.     if (err != 0) {
  108.         /*printf("Error - cannot read\n");*/
  109.         exit(-4);
  110.     }
  111.     HUnlock((Handle)myPic);
  112.     
  113.     /* release the file */
  114.     err = FSClose(fRefNum);
  115.     if (err != 0) {
  116.         /*printf("Error - cannot close file \n"); */
  117.         exit(-5);
  118.     }
  119.     
  120.     return (myPic);
  121. }
  122.  
  123.  
  124. /*===========================================================================*\
  125.  *
  126.  *    Routine:    GetPICTFile()
  127.  *
  128.  *    Comments:    Query user for PICT File
  129.  *
  130. \*===========================================================================*/
  131.  
  132. PicHandle GetPICTFile (
  133.     void)
  134. {
  135.     PicHandle             picHandle;
  136.     static SFTypeList    myTypes = { 'PICT', 0L } ;
  137.     StandardFileReply    mySFReply ;
  138.         
  139.     StandardGetFilePreview(nil, 1, myTypes, &mySFReply ) ;
  140.     
  141.     if (mySFReply.sfGood) {
  142.         picHandle = OpenPICTFile(&mySFReply.sfFile);
  143.         return (picHandle);
  144.     }
  145.     
  146.     return (0);
  147. }
  148.  
  149.  
  150. /*===========================================================================*\
  151.  *
  152.  *    Routine:    LoadMapPICT()
  153.  *
  154.  *    Comments:    take a PICT handle and loads it into a bitmap structure
  155.  *
  156. \*===========================================================================*/
  157.  
  158. short LoadMapPICT(
  159.     PicHandle             pict,
  160.     unsigned long         mapID,
  161.     unsigned long         mapSizeX,
  162.     unsigned long         mapSizeY,
  163.     TQ3StoragePixmap     *bMap)
  164. {
  165.     unsigned long             *textureMap;
  166.     unsigned long            *textureMapAddr;
  167.     unsigned long             *pictMap;
  168.     unsigned long            pictMapAddr;
  169.     register unsigned long     row;
  170.     register unsigned long     col;
  171.     Rect                     rectGW;
  172.     GWorldPtr                 pGWorld;
  173.     PixMapHandle             hPixMap;
  174.     unsigned long             pictRowBytes;
  175.     QDErr                    err;
  176.     GDHandle                oldGD;
  177.     GWorldPtr                oldGW;
  178.     short                    success;
  179.     
  180.     mapID;        /* unused argument */
  181.  
  182.     textureMapAddr = NULL;
  183.     pGWorld = NULL;
  184.  
  185.     /* save current port */
  186.     GetGWorld(&oldGW, &oldGD);
  187.  
  188.     /* create the GWorld */
  189.     SetRect(&rectGW, 0, 0, (unsigned short)mapSizeX, (unsigned short)mapSizeY);
  190.  
  191.     err = NewGWorld(&pGWorld, 32, &rectGW, 0, 0, useTempMem);
  192.     if (err != noErr)
  193.         return 0;
  194.  
  195.     success = 1;
  196.     
  197.     hPixMap = GetGWorldPixMap(pGWorld);
  198.     pictMapAddr = (unsigned long)GetPixBaseAddr (hPixMap);
  199.     pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
  200.     
  201.     /* put the PICT into the window */
  202.     SetGWorld(pGWorld, nil);
  203.     
  204.     LockPixels(hPixMap);
  205.     EraseRect(&rectGW);
  206.     DrawPicture(pict, &rectGW);
  207.         
  208.     /* allocate an area of memory for the texture */
  209.     textureMap = (unsigned long *)malloc(mapSizeX * mapSizeY * sizeof(unsigned long));
  210.     if (textureMap == NULL) {
  211.         success = 0;
  212.         goto bail;
  213.     }
  214.     /* bMap->image = (char *)textureMap; */
  215.  
  216.     /* copy the PICT into the texture */
  217.     textureMapAddr = textureMap;
  218.     for (row = 0L; row < mapSizeY; row++) {
  219.         pictMap = (unsigned long *)(pictMapAddr + (pictRowBytes * row));
  220.         for (col = 0L; col < mapSizeX; col++) {
  221.             *textureMap++ = (*pictMap++ | 0xff000000L);
  222.         }
  223.     }
  224.         
  225.     bMap->image = Q3MemoryStorage_New((const unsigned char *)textureMapAddr, 
  226.                                   mapSizeX * mapSizeY * sizeof(unsigned long));
  227.                                   
  228.     if (bMap->image == NULL) {
  229.         /* error */
  230.         success = 0;
  231.         goto bail;
  232.     }
  233.  
  234.     UnlockPixels(hPixMap);
  235.     
  236.     bMap->width     = mapSizeX;
  237.     bMap->height    = mapSizeY;
  238.     bMap->rowBytes     = bMap->width * 4;
  239.     bMap->pixelSize = 32;
  240.     bMap->pixelType    = kQ3PixelTypeRGB32;
  241.     bMap->bitOrder    = kQ3EndianBig;
  242.     bMap->byteOrder    = kQ3EndianBig;
  243.     
  244.     /* Free junk */
  245. bail:
  246.  
  247.     SetGWorld(oldGW, oldGD);
  248.     if (pGWorld != NULL)
  249.         DisposeGWorld(pGWorld);
  250.     
  251.     if (textureMapAddr != NULL)
  252.         free(textureMapAddr);
  253.     
  254.     return success;
  255. }
  256.  
  257.  
  258.  
  259.  
  260. /*===========================================================================*\
  261.  *
  262.  *    Routine:    LoadMapPICTCompressed()
  263.  *
  264.  *    Comments:    take a PICT handle and loads it into a compressed pixmap structure
  265.  *
  266. \*===========================================================================*/
  267.  
  268. short LoadMapPICTCompressed(PicHandle pict, unsigned long mapID, unsigned long    mapSizeX,
  269.                             unsigned long mapSizeY, TQ3CompressedPixmap    *bMap)
  270. {
  271.     unsigned long            pictMapAddr;
  272.     Rect                     rectGW;
  273.     GWorldPtr                 pGWorld = NULL;
  274.     PixMapHandle             hPixMap;
  275.     unsigned long             pictRowBytes;
  276.     QDErr                    err;
  277.     GDHandle                oldGD;
  278.     GWorldPtr                oldGW;
  279.     short                    success;
  280.     TQ3Status                status;
  281.     
  282.     mapID;                                                /* unused argument */
  283.  
  284.  
  285.     GetGWorld(&oldGW, &oldGD);                            /* save current port */
  286.  
  287.  
  288.             /* CREATE THE GWORLD */
  289.     
  290.     SetRect(&rectGW, 0, 0, (unsigned short)mapSizeX, (unsigned short)mapSizeY);
  291.     err = NewGWorld(&pGWorld, 32, &rectGW, 0, 0, useTempMem);
  292.     if (err != noErr)
  293.         return 0;
  294.     success = 1;
  295.     
  296.             /* GET GWORLD INFO */
  297.             
  298.     hPixMap = GetGWorldPixMap(pGWorld);
  299.     pictMapAddr = (unsigned long)GetPixBaseAddr (hPixMap);
  300.     pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
  301.     
  302.     
  303.             /* DRAW PICT INTO GWORLD */
  304.             
  305.     SetGWorld(pGWorld, nil);    
  306.     LockPixels(hPixMap);
  307.     EraseRect(&rectGW);
  308.     DrawPicture(pict, &rectGW);
  309.         
  310.         
  311.             /* FILL OUT COMPRESSED PIXMAP STRUCTURE */
  312.  
  313.     bMap->compressedImage     = nil;
  314.     bMap->imageDesc         = nil;
  315.     bMap->makeMipmaps         = kQ3True;    
  316.     bMap->width             = mapSizeX;
  317.     bMap->height            = mapSizeY;
  318.     bMap->pixelSize         = 32;
  319.     bMap->pixelType         = kQ3PixelTypeRGB32;
  320.         
  321.         
  322.         /* COMPRESS IMAGE AND FILL OUT REMAINING RECORDS IN STRUCTURE */
  323.             
  324.     status = Q3CompressedPixmapTexture_CompressImage(bMap, hPixMap, kJPEGCodecType, anyCodec,
  325.                                                     32, codecNormalQuality);
  326.     if (status == kQ3Failure)
  327.         success = 0;
  328.         
  329.         
  330.     /* Free junk */
  331. bail:
  332.  
  333.     SetGWorld(oldGW, oldGD);
  334.     if (pGWorld != NULL)
  335.         DisposeGWorld(pGWorld);
  336.         
  337.     return success;
  338. }
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.